package com.sponge.controller;


import com.sponge.constant.ContractConstant;
import com.sponge.contract.Credit_sol_Credit;
import com.sponge.dao.imp.CreditEntityDaoImpl;
import com.sponge.result.ApiError;
import com.sponge.result.ServiceResult;
import com.sponge.result.SpongeException;
import com.sponge.utils.IdentityCardValidator;
import com.sponge.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.tx.Transfer;

import java.math.BigInteger;
import java.util.concurrent.ExecutionException;

@RestController
public class CreditEntityController {

    @Autowired
    private IdentityCardValidator identityCardValidator;

    @Autowired
    private CreditEntityDaoImpl creditEntityDao;

    @Autowired
    private Web3j web3j;

//    @Autowired
//    @Qualifier("testConfiguration")
//    private CommonConfig commonConfig;

    @RequestMapping("/entity_register")
    public ServiceResult<String> creditEntityRegister(@RequestParam String username,
                                                      @RequestParam String identityNumber,
                                                      @RequestParam String password,
                                                      @RequestParam String verifyCode) throws SpongeException {
        LogUtil.info(CreditEntityController.class,"username==" + username + "identityNumber==" + identityNumber + "password==" + password + "verifyCode==" + verifyCode);
//        username==观浩identityNumber==430221197404093202password==12345678verifyCode==123456
        if (verifyCode.equals("123456")){
            boolean validate = identityCardValidator.isValidatedAllIdcard(identityNumber);
            if (validate){
                boolean addSuccess = creditEntityDao.insertCreditEntity(identityNumber, username,password);
                StringBuffer contractPassword = new StringBuffer();
                contractPassword.append(ContractConstant.USER_PASSWORD_PREFIX).append(username).append(password);
                Credentials credentials = Credentials.create("8b2b01d2b6ba879bfa65f9fb80939e2e53c6ff851ab2abe0c521b8b7d2a02068");
                try {
                    Credit_sol_Credit credit_sol_credit = Credit_sol_Credit.deploy(web3j, credentials, Transfer.GAS_PRICE, BigInteger.valueOf(2000000L)).sendAsync().get();
//                    credit_sol_credit.addAgency("")
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }

                return ServiceResult.getResult(addSuccess);
            }else {
                throw new SpongeException(ApiError.INVALID_IDENTITY_CARD_NUMBER);
            }
        }else{
            throw new SpongeException(ApiError.VERIFY_CODE_ERROR);
        }
    }



}
